package org.easy.tool.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 返回值类
 * @author Valley
 * Created by Valley on 2023/7/14 16:36
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R {



    private int code;
    private String msg;
    private Object data;

    /**
     * 功能：请求成功，无返回值
     */
    public static R ok(){
        try {
            return create(Status.SUCCESS_CODE, Status.SUCCESS_MSG, null);
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 功能：请求成功，有返回值
     */
    public static R ok(Object data){
        try {
            return create(Status.SUCCESS_CODE, Status.SUCCESS_MSG, data);
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 功能：请求成功，有返回值
     */
    public static R ok(String msg, Object data){
        try {
            return create(Status.SUCCESS_CODE, msg, data);
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 功能：请求成功，有返回值
     */
    public static R ok(int code, String msg, Object data){
        try {
            return create(code, msg, data);
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 功能：请求失败返回值
     */
    public static R fail(){
        try {
            return create(Status.FAIL_CODE, Status.FAIL_MSG, null);
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 功能：请求失败返回值
     */
    public static R fail(String msg) {
        return create(Status.FAIL_CODE, msg, null);
    }

    /**
     * 功能：请求失败返回值
     */
    public static R fail(int code, String msg) {
        return create(code, msg, null);
    }

    /**
     * 功能：请求返回值
     */
    private static R create(int code, String msg, Object data){
        R result = new R();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

}
